<?php

/* 
 * siyueweb.cn 多多房产
 * @author 111 经纪人管理
 */
defined('IN_IA') or exit('Access Denied');
global $_GPC, $_W;
load()->func('tpl');
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';	
$selects = $this->_forms;
$sec = $this->module_setting();
$uniacid = $_W['uniacid']; 
$openid = $_W['openid'];

$table_borker = 'kbwdl_share_user';
/*判断操作权限*/
$opersql = '';
if($_W['user']['type']==3){    
    $opersql = " AND `uid` =:uid ";
}
/*海报推广的客户列表*/
if($operation =='display'){
    $pindex = max(1, intval($_GPC['page']));
    $psize = 15;
    
     $condition = ' WHERE `uniacid` = :uniacid   ';
    /*类型*/
    $rt = intval($_GPC['rt']);
 
    $params = array(':uniacid' => $uniacid );
    if (!empty($_GPC['keyword'])) {
            $condition .= ' AND `name` LIKE :username';
            $params[':username'] = '%' . trim($_GPC['keyword']) . '%';
    }
    if($opersql){
        $condition .= $opersql;
        $params[':uid'] = $_W['uid'];
    }
      $sql = 'SELECT COUNT(*) FROM ' . tablename($table_borker). $condition;
    $total = pdo_fetchcolumn($sql, $params);
    $bmember = [];
    if (!empty($total)) {
             $sql = 'SELECT * FROM ' .tablename($table_borker) . $condition . ' ORDER BY  id desc LIMIT ' . ($pindex - 1) * $psize . ',' . $psize;
            $list = pdo_fetchall($sql, $params);
            $pager = pagination($total, $pindex, $psize);
            $dot = $str = "";
            foreach($list as $key => $val){
                if($val['uid']){
                    $str.= $dot. $val['uid'];
                    $dot = ",";
                }
            }
            if($str){
                $sql = "select uid, nickname, avatar from ".tablename("mc_members")." where uid in ($str)";
                $ms =  pdo_fetchall($sql);
              
                foreach($ms as $k => $v){
                    $bmember[$v['uid']] = $v;
                }                
               foreach($list as $key => $val){
                    if(empty($val['name'])){
                        $val['nickname'] = $bmember[$val['uid']]['nickname'];
                    }
                    if(empty($val['avatar'])){
                        $val['avatar'] = $bmember[$val['uid']]['avatar'];
                    }
                    $list[$key] = $val;
                }  
            }
            
           
    } 
 
}
/*申请佣金的记录*/
if($operation =='yongjin'){
    
    if(isset($_GPC['ocpid']) && !empty($_GPC['ocpid'])){
        $ocpid = $_GPC['ocpid'];
        pdo_update("kbwdl_actlog", array('num'=>0), array('id'=> $ocpid, 'uniacid'=>$uniacid));
        itoast("处理成功", "refresh","success");
    }
    $pindex = max(1, intval($_GPC['page']));
    $psize = 15;
    
     $condition =  " WHERE `uniacid` = :uniacid  and actname='agentMoney' and acttype=51 ";
    /*类型*/
    $rt = intval($_GPC['rt']);
 
    $params = array(':uniacid' => $uniacid );
    if (!empty($_GPC['keyword'])) {
            $condition .= ' AND `note` LIKE :note';
            $params[':note'] = '%' . trim($_GPC['keyword']) . '%';
    }
 
    $sql = 'SELECT COUNT(*) FROM ' . tablename("kbwdl_actlog"). $condition;
    $total = pdo_fetchcolumn($sql, $params);
    $bmember = [];
    if (!empty($total)) {
             $sql = 'SELECT * FROM ' .tablename("kbwdl_actlog") . $condition . ' ORDER BY  id desc LIMIT ' . ($pindex - 1) * $psize . ',' . $psize;
            $list = pdo_fetchall($sql, $params);
            $pager = pagination($total, $pindex, $psize);
            $dot = $str = "";
            foreach($list as $key => $val){
                if($val['uid']){
                    $str.= $dot. $val['uid'];
                    $dot = ",";
                }
            }
            if($str){
                $sql = "select uid, nickname, avatar from ".tablename("mc_members")." where uid in ($str)";
                $ms =  pdo_fetchall($sql);
              
                foreach($ms as $k => $v){
                    $bmember[$v['uid']] = $v;
                }                
               foreach($list as $key => $val){
                    if(empty($val['name'])){
                        $val['nickname'] = $bmember[$val['uid']]['nickname'];
                    }
                    if(empty($val['avatar'])){
                        $val['avatar'] = $bmember[$val['uid']]['avatar'];
                    }
                    $list[$key] = $val;
                }  
            }
            
           
    } 
 
} 
include $this->template('share_list');

/**
 * 查找到模块的管理员账号
 */
function get_shop_permission($uniacid){
    global $_W,$GPC; 
    $opersql = '';
    if($_W['user']['type']==3){    
        $opersql = " AND  `uid` ='".$_W['uid']."' ";        
    }
    
    $role = pdo_fetchall("select shopname,id from ".tablename("kbwdl_shop").
            "  where uniacid='$uniacid'  $opersql");
    $admin = array();
    if(!empty($role)){
        foreach($role as $key=>$r){
            $admin[$r['id']] = $r['shopname'];
        }
    }
    return $admin;
}